package com.clstu.qqclient.qqframe;

import com.clstu.qqclient.service.UserClientService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * 这是一个用于发送文件的窗口
 */
public class SentFileFrame extends JFrame implements ActionListener,KeyListener {

    private JLabel getterLb = new JLabel("发送给");
    private JTextField getter = new JTextField(60);
    private JLabel pathLb = new JLabel("请输入文件路径");
    private JTextField path = new JTextField(60);
    private JLabel destLb = new JLabel("请输入目标路径");
    private JTextField dest = new JTextField(60);
    private JButton btSent = new JButton("发送");
    private JButton btExit = new JButton("退出");
    private String userID;//表示打开该窗口的用户

    public SentFileFrame(String userID){
        super("用户"+userID+"正在发送文件");//窗口名称
        this.userID = userID;
        this.setLayout(new FlowLayout());//设置流式布局
        this.add(getterLb);//在窗口中加入这些成员组件
        this.add(getter);
        this.add(pathLb);
        this.add(path);
        this.add(destLb);
        this.add(dest);
        this.add(btSent);
        this.add(btExit);

        this.setSize(700,230);//设置一些窗口属性
        this.setLocation(700,400);
        this.setResizable(true);
        this.setVisible(true);

        btExit.addActionListener(this);//给各个按钮加入监听器
        btSent.addActionListener(this);
        dest.addKeyListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {//事件监听的处理

        if(e.getSource()==btSent){//如果是发送按键被按下
            sentFile();
        }else if(e.getSource()==btExit){//如果是退出按键被按下
            this.dispose();
        }

    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            sentFile();
        }
    }
    @Override
    public void keyReleased(KeyEvent e) { }

    private void sentFile() {
        String getter = this.getter.getText();
        String path = this.path.getText();
        String dest = this.dest.getText();
        UserClientService.sentFile(userID,getter,path,dest);
        JOptionPane.showMessageDialog(this,"发送成功");
        this.dispose();
    }

}
